import pandas as pd
from reportlab.lib.pagesizes import A4
from reportlab.pdfgen import canvas
from reportlab.lib.units import mm
from reportlab.pdfbase import pdfmetrics
from reportlab.pdfbase.ttfonts import TTFont
from reportlab.lib.styles import getSampleStyleSheet, ParagraphStyle
from reportlab.platypus import Paragraph
from reportlab.lib.enums import TA_CENTER

# 设置页面尺寸为A4纵向模式
PAGE_WIDTH, PAGE_HEIGHT = A4

# 边距设置（单位：毫米）
MARGIN_LEFT = 20 * mm  # 左边距
MARGIN_RIGHT = 20 * mm  # 右边距
MARGIN_TOP = 20 * mm  # 上边距
MARGIN_BOTTOM = 20 * mm  # 下边距

# 字体大小
FONT_SIZE = 50

# 每页的名字数量
COL_NUM_PER_PAGE = 2  # 每页列数
ROW_NUM_PER_PAGE = 4  # 每页行数
NAMES_PER_PAGE = COL_NUM_PER_PAGE * ROW_NUM_PER_PAGE  # 每页姓名数

# 注册中文字体
font_path = '/Library/Fonts/仿宋_GB2312.ttf'  # 替换为你的字体文件路径
pdfmetrics.registerFont(TTFont('GB2312', font_path))


def draw_names_on_page(c, names_array):
    styles = getSampleStyleSheet()
    style_center = ParagraphStyle(
        'centered',
        parent=styles['Normal'],
        fontSize=FONT_SIZE,
        leading=FONT_SIZE + 2,  # 行间距
        fontName='GB2312',
        alignment=TA_CENTER  # 居中对齐
    )

    width, height = PAGE_WIDTH - MARGIN_LEFT - MARGIN_RIGHT, PAGE_HEIGHT - MARGIN_TOP - MARGIN_BOTTOM

    col_width = width / COL_NUM_PER_PAGE  # 分成两列
    row_height = (height - (ROW_NUM_PER_PAGE - 1) * 5 * mm) / ROW_NUM_PER_PAGE  # 每行高度，加上额外的间距

    for i, name in enumerate(names_array):
        col = i % COL_NUM_PER_PAGE  # 计算列号
        row = i // COL_NUM_PER_PAGE  # 计算行号

        x = MARGIN_LEFT + col * col_width
        y = PAGE_HEIGHT - MARGIN_TOP - (row + 1) * row_height
        rect_width = col_width
        rect_height = row_height

        # 绘制每个名字周围的边框
        c.setStrokeColorRGB(0, 0, 0)  # 设置边框颜色为黑色
        c.rect(x, y, rect_width, rect_height, stroke=1, fill=0)  # stroke=1绘制边框 fill=0不填充

        # 计算文本居中的位置
        p = Paragraph(name, style_center)
        text_width, text_height = p.wrapOn(c, col_width, row_height)

        # 居中显示名字
        text_x = x + (rect_width - text_width) / 2
        text_y = y + (rect_height - text_height) / 2
        p.drawOn(c, text_x, text_y)


def add_border_and_crop_marks(c):
    # 添加裁剪标记（如果需要全局裁剪标记可以保留此函数）

    crop_mark_length = 10 * mm  # 裁剪标记长度
    crop_mark_offset = 5 * mm  # 裁剪标记距离页面边缘的距离

    # 左上角裁剪标记
    c.line(MARGIN_LEFT - crop_mark_length, PAGE_HEIGHT - MARGIN_TOP + crop_mark_offset,
           MARGIN_LEFT - crop_mark_offset, PAGE_HEIGHT - MARGIN_TOP + crop_mark_offset)
    c.line(MARGIN_LEFT - crop_mark_length, PAGE_HEIGHT - MARGIN_TOP - crop_mark_offset,
           MARGIN_LEFT - crop_mark_offset, PAGE_HEIGHT - MARGIN_TOP - crop_mark_offset)

    # 右上角裁剪标记
    c.line(PAGE_WIDTH - MARGIN_RIGHT + crop_mark_offset, PAGE_HEIGHT - MARGIN_TOP + crop_mark_offset,
           PAGE_WIDTH - MARGIN_RIGHT + crop_mark_length, PAGE_HEIGHT - MARGIN_TOP + crop_mark_offset)
    c.line(PAGE_WIDTH - MARGIN_RIGHT + crop_mark_offset, PAGE_HEIGHT - MARGIN_TOP - crop_mark_offset,
           PAGE_WIDTH - MARGIN_RIGHT + crop_mark_length, PAGE_HEIGHT - MARGIN_TOP - crop_mark_offset)

    # 左下角裁剪标记
    c.line(MARGIN_LEFT - crop_mark_length, MARGIN_BOTTOM - crop_mark_offset,
           MARGIN_LEFT - crop_mark_offset, MARGIN_BOTTOM - crop_mark_offset)
    c.line(MARGIN_LEFT - crop_mark_length, MARGIN_BOTTOM + crop_mark_offset,
           MARGIN_LEFT - crop_mark_offset, MARGIN_BOTTOM + crop_mark_offset)

    # 右下角裁剪标记
    c.line(PAGE_WIDTH - MARGIN_RIGHT + crop_mark_offset, MARGIN_BOTTOM - crop_mark_offset,
           PAGE_WIDTH - MARGIN_RIGHT + crop_mark_length, MARGIN_BOTTOM - crop_mark_offset)
    c.line(PAGE_WIDTH - MARGIN_RIGHT + crop_mark_offset, MARGIN_BOTTOM + crop_mark_offset,
           PAGE_WIDTH - MARGIN_RIGHT + crop_mark_length, MARGIN_BOTTOM + crop_mark_offset)


def generate_pdf(filename, names_list):
    c = canvas.Canvas(filename, pagesize=A4)
    buffer = []

    for idx, name in enumerate(names_list, start=1):
        buffer.append(name)
        if len(buffer) == NAMES_PER_PAGE or idx == len(names_list):
            draw_names_on_page(c, buffer)
            # add_border_and_crop_marks(c)  # 添加全局裁剪标记（可选）
            c.showPage()  # 结束当前页面并开始新页面
            buffer.clear()

    c.save()


def read_excel(file_path, sheet_name=0):
    # 使用pandas读取Excel文件
    df = pd.read_excel(file_path, sheet_name=sheet_name)
    return df['姓名'].dropna().tolist()  # 假设名单在'姓名'这一列中


if __name__ == "__main__":
    # 从Excel文件中读取名单
    excel_file_path = '/Users/lidaoyu/Downloads/工作簿1.xlsx'  # 替换为你的Excel文件路径
    names = read_excel(excel_file_path)

    # 生成PDF文件
    pdf_output_filename = "temp/output.pdf"
    generate_pdf(pdf_output_filename, names)
